import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

import {formatTime} from '../../utils/util'
var amapFile = require('../../libs/amap-wx.130');

const request = require("../../utils/request")

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    interval:1500,//自动切换时间间隔
    duration:1000,//滑动动画时长
    nearestPoint:"紫阳公园",
    nearestDistance:"265km",
    notifyNum:0,
    systemNoticeNum:0,
    personalNoticeNum:0,
    systemNoticeList:[],
    personalNoticeList:[],
    addressName:'',
    //static
    mapBack:'',
    logo:'',
    xuekeyoushi:'',
    shangwujihua:'',
    lvsefazhan:'',
    shangmenfuwu:'',
    swiperList: [{
    }],
    cardCur:0
  },
  getRegeo:function()
  {
    var that = this;
    var myAmapFun = new amapFile.AMapWX({key:'4f2354b3309e2b695928d67e2ad965be'});
    myAmapFun.getRegeo({
      success: function(data){
        console.log(data[0])
        const addressName = data[0].name;
        that.setData({
          addressName
        })
        //成功回调
      },
      fail: function(info){
        //失败回调
        console.log(info)
      }
    })
  },


  cardSwiper(e) {
    // console.log(e.detail.current)
    this.setData({
      cardCur: e.detail.current
    })
  },
  gotoMap()
  {
    wx.navigateTo({
      url: './map/map',
    })
  },
  gotoMessageCenter()
  {
    if(!app.globalData.userInfo)
    {
      Dialog.alert({
        message: '请您先登录',
      }).then(() => {
        wx.switchTab({
          url: '../../pages/mine/index',
        })
      });
    }else
    {
      const systemNoticeListstr = JSON.stringify(this.data.systemNoticeList)
      const personalNoticeListstr = JSON.stringify(this.data.personalNoticeList)
      const systemNoticeNum=this.data.systemNoticeNum
      const personalNoticeNum=this.data.personalNoticeNum
      // systemNoticeNum,
      // personalNoticeNum,
      // systemNoticeList,
      // personalNoticeList
      wx.navigateTo({
        url: `../messageCenter/index?systemNoticeNum=${systemNoticeNum}&personalNoticeNum=${personalNoticeNum}&systemNoticeList=${systemNoticeListstr}&personalNoticeList=${personalNoticeListstr}`
      })
    }
  },
  gotoHelpCenter()
  {
    wx.navigateTo({
      url: '../helpCenter/index',
    })
  },
  gotoDailyCheck()
  {
    if(!app.globalData.userInfo)
    {
      Dialog.alert({
        message: '请您先登录',
      }).then(() => {
        wx.switchTab({
          url: '../../pages/mine/index',
        })
      });
    }else
    {
      wx.navigateTo({
        url: '/pages/index/dailyCheck/index',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function (options) {
    this.getRegeo()
    // let res = await Promise.all([request.downloadFile('static/kaipin2.png'),request.downloadFile('static/indexbanner/1.jpg'),request.downloadFile('static/indexbanner/2.jpg'),request.downloadFile('static/indexbanner/3.jpg')])
    // this.setData({
    //   logo:res[0],
    //   banner1:res[1],
    //   banner2:res[2],
    //   banner3:res[3],
    // })
    let res2 = await request.downloadFile('static/kaipin2.png')
    this.setData({
      logo:res2
    })
    let res7 =await request.downloadFile('static/indexbanner/1.jpg')
    this.setData({
      banner1:res7
    })
    let res8 =await request.downloadFile('static/indexbanner/2.jpg')
    this.setData({
      banner2:res8,
    })
    let res9 =await request.downloadFile('static/indexbanner/3.jpg')
    this.setData({
      banner3:res9
    })
    // this.setData({
    //   logo:res2,
    //   banner1:res7,
    //   banner2:res8,
    //   banner3:res9,
    // })


    let res1 = await request.downloadFile('static/map.png')
    this.setData({
      mapBack:res1,
    })
    let res3 = await request.downloadFile('static/lvsefazhan.png')
    let res4 = await request.downloadFile('static/shangmenfuwu.png')
    let res5 = await request.downloadFile('static/shangwujihua.png')
    let res6 = await request.downloadFile('static/xuekeyoushi.png')
    this.setData({
      lvsefazhan:res3,
      shangmenfuwu:res4,
      shangwujihua:res5,
      xuekeyoushi:res6,
    })


    },


  scannService()
  {
    if(!app.globalData.userInfo)
    {
      Dialog.alert({
        message: '请您先登录',
      }).then(() => {
        wx.switchTab({
          url: '../../pages/mine/index',
        })
      });
    }
    else{
    // console.log("调起扫码")
      wx.scanCode({
        success:async (res)=> {

          Toast.loading({
            message: '正在上传订单...',
            forbidClick: true,
          });

          // console.log(res)     
          let result = res.result     //这里是扫码拿到的返回值加粗样式
          let resultArr = result.split(';')
          // console.log(resultArr)
          const bucketId=resultArr[0].split(':')[1]
          const place = resultArr[1].split(':')[1]
          const addrNo = resultArr[2].split(':')[1]
          const category = resultArr[3].split(':')[1]
          // console.log(addrNo,place,category)

          let res1 = await request.bucketInterface(bucketId,'getById',null,null,null,null,null,null,null,null)
          let bucketInfo = res1.data.data.value
          if(bucketInfo.isFull==1)
          {
            Dialog.alert({
              message: '垃圾桶已满',
            }).then(() => {
              wx.switchTab({
                url: '../../pages/index/index',
              })
            });
          }
          else{
            let result1 = await request.deliverorderInterface(addrNo,category,null,null,null,null,place,    formatTime(new Date()),app.globalData.openid,0,'add',bucketId)
            // console.log(result1)
            
            if(bucketInfo.maxWeight<bucketInfo.weight+5)
            {
              let res2 = await request.bucketInterface(bucketId,'update',bucketInfo.weight+5 ,null,null,1,null,null,null,null)
            }
            else
            {
              let res2 = await request.bucketInterface(bucketId,'update',bucketInfo.weight+5 ,null,null,0,null,null,null,null)
            }
            
            if(result1.data.status==200)
            {
              wx.navigateTo({
                url: `./deliverOrder/printing/index?orderId=${result1.data.data.value.orderId}`,
              })
            }
        }
        },
        fail(res)
        {
          console.log(res)
        }
      })
    }
  },
  gotoDoorService()
  {
    if(!app.globalData.userInfo)
    {
      Dialog.alert({
        message: '请您先登录',
      }).then(() => {
        wx.switchTab({
          url: '../../pages/mine/index',
        })
      });
    }
    else{
      wx.navigateTo({
        url: './doorService/makeChoice/index',
      })
      }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:async function () {
    let res = await request.noticeUserInterface(undefined,undefined,undefined,app.globalData.openid,"getAllNotice")
    // console.log(res.data.data.value)
    let systemNoticeNum=0,personalNoticeNum=0,systemNoticeList=[],personalNoticeList=[]
    res.data.data.value.map(async (item,index)=>{
      // console.log("通知和用户关系",item)
      let res1 = await request.noticeInterface(item.systemNoticeId,undefined,"getNoticeContent")
      // console.log("通知详细内容",res1.data.data.value)
      const noticeContent = res1.data.data.value 
      let contentArr = noticeContent.content.split('"')
      // console.log(contentArr)
      switch(contentArr[1])
      {
        case "系统通知":
          if(item.isChecked==0)
            systemNoticeNum++
          systemNoticeList.push({
            title:contentArr[1],
            content:contentArr[3]
          })
          // console.log("新增系统通知",systemNoticeNum,systemNoticeList)
          break;
        case "他人消息":
          if(item.isChecked==0)
            personalNoticeNum++
            personalNoticeList.push({
            title:contentArr[1],
            content:contentArr[3]
          })
          // console.log("新增他人消息",personalNoticeNum,personalNoticeList)
          break;
      }
      if(index==res.data.data.value.length-1)
      {
        // console.log("setdata")
        this.setData({
          systemNoticeNum,
          personalNoticeNum,
          systemNoticeList,
          personalNoticeList,
          notifyNum:systemNoticeNum+personalNoticeNum
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})